package com.android.webrtcsdkdev;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.IntDef;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;

public class ControlDialog implements View.OnClickListener {


    private Dialog dialog;
    private OnMenuClickListener onMenuClickListener;
    private RelativeLayout rl_btn_exit;
    private ArrayList<String> educationList = new ArrayList<>();
    private String selectEducation;
    private Context mContext;


    public ControlDialog setOnMenuClickListener(OnMenuClickListener onMenuClickListener) {
        this.onMenuClickListener = onMenuClickListener;
        return this;
    }


    /**
     * @param context
     */
    public void show(final Context context) {
        try {
            mContext = context;
            dialog = new Dialog(context, R.style.CloudControl);
            dialog.setContentView(R.layout.control);


            dialog.findViewById(R.id.iv_app_volume_down).setOnClickListener(this);
            dialog.findViewById(R.id.iv_app_volume_up).setOnClickListener(this);
            dialog.findViewById(R.id.linear_speed).setOnClickListener(this);
            dialog.findViewById(R.id.linear_high_speed).setOnClickListener(this);
            dialog.findViewById(R.id.linear_standard).setOnClickListener(this);
            dialog.findViewById(R.id.linear_hd).setOnClickListener(this);
            dialog.findViewById(R.id.linear_sup_hd).setOnClickListener(this);
            dialog.findViewById(R.id.tv_function_setting).setOnClickListener(this);

            dialog.findViewById(R.id.iv_fictitious).setOnClickListener(this);
            dialog.findViewById(R.id.tv_left_finger_open).setOnClickListener(this);
            if (SharedPHelper.getInstance(context).getBooleanWithDefaultTrue(VideoConstant.BOTTOM_SWITCH)) {
                dialog.findViewById(R.id.iv_fictitious).setBackground(context.getResources().getDrawable(R.drawable.shape_cloud_phone_dialog_bt_blue_bg));
            } else {
                dialog.findViewById(R.id.iv_fictitious).setBackground(context.getResources().getDrawable(R.drawable.shape_cloud_phone_dialog_bt_bg));
            }
            if (SharedPHelper.getInstance(mContext).getBooleanWithDefaultTrue(VideoConstant.BOTTOM_LEFT_SWITCH)) {
                dialog.findViewById(R.id.tv_left_finger_open).setBackground(mContext.getResources().getDrawable(R.drawable.shape_cloud_phone_dialog_bt_blue_bg));
                Drawable top = mContext.getResources().getDrawable(R.mipmap.ic_left_finger_open);
                top.setBounds(0, 0, top.getMinimumWidth(), top.getMinimumHeight());
                dialog.<TextView>findViewById(R.id.tv_left_finger_open).setCompoundDrawables(null, top, null, null);
                SharedPHelper.getInstance(mContext).putBoolean(VideoConstant.BOTTOM_LEFT_SWITCH, true);
            } else {
                dialog.findViewById(R.id.tv_left_finger_open).setBackground(mContext.getResources().getDrawable(R.drawable.shape_cloud_phone_dialog_bt_bg));
                Drawable top = mContext.getResources().getDrawable(R.mipmap.ic_left_finger_close);
                top.setBounds(0, 0, top.getMinimumWidth(), top.getMinimumHeight());
                dialog.<TextView>findViewById(R.id.tv_left_finger_open).setCompoundDrawables(null, top, null, null);
                SharedPHelper.getInstance(mContext).putBoolean(VideoConstant.BOTTOM_LEFT_SWITCH, false);
            }
            rl_btn_exit = dialog.findViewById(R.id.rl_btn_exit);
            rl_btn_exit.setOnClickListener(this);
            // 显示
            dialog.show();
        } catch (Exception e) {
            Log.d("ControlDialog","e:"+e.getMessage());
        }
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.linear_speed)
            onMenuClickListener.onMenuClick(VideoConstant.BITRATE_EXTREME_SPEED, null);
        else if (id == R.id.linear_high_speed)
            onMenuClickListener.onMenuClick(VideoConstant.BITRATE_HIGH_SPEED, null);
        else if (id == R.id.linear_standard)
            onMenuClickListener.onMenuClick(VideoConstant.BITRATE_STANDARD_CLEARING, null);
        else if (id == R.id.linear_hd)
            onMenuClickListener.onMenuClick(VideoConstant.BITRATE_HD, null);
        else if (id == R.id.linear_sup_hd)
            onMenuClickListener.onMenuClick(VideoConstant.BITRATE_SUP_HD, null);
        else if (id == R.id.iv_app_volume_down)
            onMenuClickListener.onMenuClick(VideoConstant.APP_VOLUME_DOWN, null);
        else if (id == R.id.iv_app_volume_up)
            onMenuClickListener.onMenuClick(VideoConstant.APP_VOLUME_UP, null);
        else if (id == R.id.rl_btn_exit)
            onMenuClickListener.onMenuClick(VideoConstant.BTN_EXIT, null);
        else if (id == R.id.iv_fictitious) {
//                if (cloudPhoneActionEntity != null) {
//                    if (cloudPhoneActionEntity.getFictitious() == 1) {
//                        cloudPhoneActionEntity.setFictitious(0);
//                        reSetCloudPhoneActionEntity(cloudPhoneActionEntity);
//                    } else {
//                        cloudPhoneActionEntity.setFictitious(1);
//                        reSetCloudPhoneActionEntity(cloudPhoneActionEntity);
//                    }
//                }
            if (SharedPHelper.getInstance(mContext).getBooleanWithDefaultTrue(VideoConstant.BOTTOM_SWITCH)) {
                dialog.findViewById(R.id.iv_fictitious).setBackground(mContext.getResources().getDrawable(R.drawable.shape_cloud_phone_dialog_bt_bg));
                SharedPHelper.getInstance(mContext).putBoolean(VideoConstant.BOTTOM_SWITCH, false);
            } else {
                dialog.findViewById(R.id.iv_fictitious).setBackground(mContext.getResources().getDrawable(R.drawable.shape_cloud_phone_dialog_bt_blue_bg));
                SharedPHelper.getInstance(mContext).putBoolean(VideoConstant.BOTTOM_SWITCH, true);
            }
            onMenuClickListener.onMenuClick(VideoConstant.SWITCH_PATTERN, null);
        } else if (id == R.id.tv_left_finger_open) {
            if (SharedPHelper.getInstance(mContext).getBooleanWithDefaultTrue(VideoConstant.BOTTOM_LEFT_SWITCH)) {
                dialog.findViewById(R.id.tv_left_finger_open).setBackground(mContext.getResources().getDrawable(R.drawable.shape_cloud_phone_dialog_bt_bg));
                Drawable top = mContext.getResources().getDrawable(R.mipmap.ic_left_finger_close);
                top.setBounds(0, 0, top.getMinimumWidth(), top.getMinimumHeight());
                dialog.<TextView>findViewById(R.id.tv_left_finger_open).setCompoundDrawables(null, top, null, null);
                SharedPHelper.getInstance(mContext).putBoolean(VideoConstant.BOTTOM_LEFT_SWITCH, false);
            } else {
                dialog.findViewById(R.id.tv_left_finger_open).setBackground(mContext.getResources().getDrawable(R.drawable.shape_cloud_phone_dialog_bt_blue_bg));
                Drawable top = mContext.getResources().getDrawable(R.mipmap.ic_left_finger_open);
                top.setBounds(0, 0, top.getMinimumWidth(), top.getMinimumHeight());
                dialog.<TextView>findViewById(R.id.tv_left_finger_open).setCompoundDrawables(null, top, null, null);
                SharedPHelper.getInstance(mContext).putBoolean(VideoConstant.BOTTOM_LEFT_SWITCH, true);
            }
            onMenuClickListener.onMenuClick(VideoConstant.APP_FINGER_CONTROL, null);
        }else if(id==R.id.tv_function_setting){
            if (SharedPHelper.getInstance(mContext).getBooleanWithDefaultTrue(VideoConstant.BOTTOM_FUNCTION_SWITCH)) {
                dialog.findViewById(R.id.tv_function_setting).setBackground(mContext.getResources().getDrawable(R.drawable.shape_cloud_phone_dialog_bt_bg));
                Drawable top = mContext.getResources().getDrawable(R.mipmap.ic_left_finger_close);
                top.setBounds(0, 0, top.getMinimumWidth(), top.getMinimumHeight());
                dialog.<TextView>findViewById(R.id.tv_function_setting).setCompoundDrawables(null, top, null, null);
                SharedPHelper.getInstance(mContext).putBoolean(VideoConstant.BOTTOM_FUNCTION_SWITCH, false);
            } else {
                dialog.findViewById(R.id.tv_function_setting).setBackground(mContext.getResources().getDrawable(R.drawable.shape_cloud_phone_dialog_bt_blue_bg));
                Drawable top = mContext.getResources().getDrawable(R.mipmap.ic_left_finger_open);
                top.setBounds(0, 0, top.getMinimumWidth(), top.getMinimumHeight());
                dialog.<TextView>findViewById(R.id.tv_function_setting).setCompoundDrawables(null, top, null, null);
                SharedPHelper.getInstance(mContext).putBoolean(VideoConstant.BOTTOM_FUNCTION_SWITCH, true);
            }
            onMenuClickListener.onMenuClick(VideoConstant.APP_FUNCTION_CONTROL, null);
        }
    }



    public interface OnMenuClickListener {
        void onMenuClick(@MenuType int menuType, String entity);
    }


    @IntDef({VideoConstant.APP_VOLUME_DOWN, VideoConstant.APP_VOLUME_UP, VideoConstant.BITRATE_EXTREME_SPEED, VideoConstant.BITRATE_HIGH_SPEED,
            VideoConstant.BITRATE_STANDARD_CLEARING, VideoConstant.BTN_EXIT, VideoConstant.BITRATE_HD, VideoConstant.BITRATE_SUP_HD,})
    @Retention(RetentionPolicy.SOURCE)
    public @interface MenuType {
    }

    public void dismissDialog() {
        try {
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }
        } catch (Exception e) {

        }
    }

    public void onDismissDialog() {
        try {
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }
        } catch (Exception e) {

        }
    }
}
